package com.meelive.ingkee.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.meelive.ingkee.push.model.Constance;
import com.meelive.ingkee.push.model.InkePushType;
import com.meelive.ingkee.push.model.ReceiverInfo;
import com.meelive.ingkee.push.model.ReceiverType;

import static com.meelive.ingkee.push.common.PushUtils.DEBUG;
import static com.meelive.ingkee.push.model.Constance.TAG;

/**
 * Create by: fanfeiyue
 * on: 2019-09-28
 * desc:
 */
public abstract class InkeBasePushReceiver extends BroadcastReceiver {

    @Override
    public final void onReceive(Context context, Intent intent) {
        if (intent == null) {
            return;
        }
        ReceiverType receiverType = (ReceiverType) intent.getSerializableExtra(Constance.RECEIVER_TYPE);
        ReceiverInfo receiverInfo = (ReceiverInfo) intent.getSerializableExtra(Constance.RECEIVER_INFO);
        if (receiverType == null || receiverInfo == null) {
            if (DEBUG) {
                Log.w(TAG, "InkeBasePushReceiver-onReceive return：receiverType:" + receiverType + ",receiverInfo:" + receiverInfo);
            }
            return;
        }
        switch (receiverType){
            case PASSTHROUGH:
                if (DEBUG){
                    Log.i(TAG, "[receiver passThrough message]，from：" + receiverInfo.pushType + ", message：" + receiverInfo.message);

                }
                onPassThroughPush(context, receiverInfo.pushType, receiverInfo.message);
                break;
            case NOTIFY_CLICK:
                if (DEBUG){
                    Log.i(TAG, "[receiver notify clicked message]，from：" + receiverInfo.pushType + ", message：" + receiverInfo.message);
                }
                onNotificationClick(context, receiverInfo.pushType, receiverInfo.message);
                break;
            case NOTIFY_ARRIVED:
                if (DEBUG){
                    Log.i(TAG, "[receiver notify arrived message]，from：" + receiverInfo.pushType + ", message：" + receiverInfo.message);
                }
                onNotificationArrived(context, receiverInfo.pushType, receiverInfo.message);
                break;
            case RECEIVER_TOKEN:
                if (DEBUG){
                    Log.i(TAG, "[receiver token]，from：" + receiverInfo.pushType + ", token：" + receiverInfo.token);
                }
                onReceiveToken(receiverInfo.pushType, receiverInfo.token);
                break;
            case ERROR:
                if (DEBUG){
                    Log.i(TAG, "[receiver errorCode]，from：" + receiverInfo.pushType + ", errorCode：" + receiverInfo.errorCode);
                }
                onRegisterError(receiverInfo.pushType, receiverInfo.errorCode);
                break;
        }
    }

    public abstract void onRegisterError(InkePushType pushType, long errorCode);

    public abstract void onReceiveToken(InkePushType pushType, String token);

    public void onPassThroughPush(Context context, InkePushType pushType, String message) {
    }

    public void onNotificationClick(Context context, InkePushType pushType, String message) {
    }

    public void onNotificationArrived(Context context, InkePushType pushType, String message) {
    }
}
